<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 17/4/25
 * Time: 下午1:50
 */

use yii\helpers\Url;

$web = Url::base();
$this->title = '管理员管理';
?>
<script type="text/javascript" src="<?= $web ?>/js/pager.js"></script>
<script type="text/javascript">
    function filter() {
        var name = $("#name").val();
        var account = $("#account").val();
        var phone = $("#phone").val();

        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (name != '' && name != null) {
            href_str = href_str + "&name=" + name;
        }
        if (account != '' && account != null) {
            href_str = href_str + "&account=" + account;
        }
        if (phone != '' && phone != null) {
            href_str = href_str + "&phone=" + phone;
        }


        window.location.href = href_str;
    }

    function onAddData(id) {
        $("#myModalEdit").modal('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }

        modalEdit(data);
    }


    function addMiniPrograms(id) {
        $("#managerMiniProgramModal").modal('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }

        $("#managerMiniProgramModalEdit").html('正在加载数据...');

        requestHtml('manager-add-mini-programs-partial', data, true, function (responseHtml) {
            $("#managerMiniProgramModalEdit").html(responseHtml);
        });

    }
    
    function saveMiniProgramData() {
        var data = {};
        data.manager_id = $("#managerMiniProgramModal").find("#manager_id").val();
        data.mini_program_ids = [];
        $("#managerMiniProgramModal").find("input[type=checkbox]").each(function () {
                if ($(this).val() != "") {
                    if ($(this).is(':checked')) {
                        data.mini_program_ids.push($(this).val());
                    }
                }
            }
        );

        requestJson("manager-save-mini-programs-handler", data, true, function (response) {
            if (response.status == 0) {
                filter();
            } else {
                errorDialog(response.message);
            }
        });

    }


    function deleteMiniPrograms(id) {
        $("#managerDeleteMiniProgramModal").modal('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }

        $("#managerDeleteMiniProgramModalEdit").html('正在加载数据...');

        requestHtml('manager-delete-mini-programs-partial', data, true, function (responseHtml) {
            $("#managerDeleteMiniProgramModalEdit").html(responseHtml);
        });
    }

    function deleteMiniProgramData() {
        var data = {};
        data.manager_id = $("#managerDeleteMiniProgramModal").find("#manager_id").val();
        data.mini_program_ids = [];
        $("#managerDeleteMiniProgramModal").find("input[type=checkbox]").each(function () {
                if ($(this).val() != "") {
                    if ($(this).is(':checked')) {
                        data.mini_program_ids.push($(this).val());
                    }
                }
            }
        );

        requestJson("manager-delete-mini-programs-handler", data, true, function (response) {
            if (response.status == 0) {
                filter();
            } else {
                errorDialog(response.message);
            }
        });

    }


    //打开新增修改页面
    function modalEdit(data) {
        $("#modaledit").html('正在加载数据...');

        requestHtml('manager-detail-partial', data, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
        });
    }



    function saveData() {
        var id = $("#manager_edit_form").find("#id").val();


        var name = $("#manager_edit_form").find("#name").val().trim();
        var account = $("#manager_edit_form").find("#account").val().trim();
        var phone = $("#manager_edit_form").find("#phone").val().trim();
        var password = $("#manager_edit_form").find("#password").val();
        var remark = $("#manager_edit_form").find("#remark").val().trim();
        var role_id = [];
        $('#manager_edit_form').find('[name="role_id[]"]:checked').each(function () {
            role_id.push($(this).val());
        });
        var is_enabled = 0;
        if ($("#manager_edit_form").find("#is_enabled").is(':checked')) {
            is_enabled = 1;
        }

        if (name == '' || name == null) {
            warningDialog('名称不能为空');
            return false;
        }

        if (validateLength(name, '名称', 4, 128) == false) {
            return false;
        }


        if (account == '' || account == null) {
            warningDialog('账号不能为空');
            return false;
        }
        if ((validateLength(account, '账号', 4, 128) == false)) {
            return false;
        }

        //验证账号的唯一性


        if (password != undefined) {
            password = password.trim();
            if (validatePassword(password) == false) {
                return false;
            }
        }

        if (validatePhone(phone) == false) {
            warningDialog('电话号码不规范');
            return false;
        }

        if (validateLength(remark, '备注(选填)', 1, 250, 1) === false) {
            return false;
        }

        var data = {};
        data.id = id;
        data.name = name;
        data.account = account;
        data.phone = phone;
        if (password != undefined) {
            data.password = password.trim();
        }
        data.remark = remark;
        data.is_enabled = is_enabled;
        data.role_id = role_id;
        requestJson("manager-save-handler", data, true, function (response) {
            if (response.status == 0) {
                filter();
            } else {
                errorDialog(response.message);
            }
        });
    }

    function deleteManager(id) {
        confirmDialog(id, null, function (id) {
            var data = {};
            data.id = id;
            requestJson("manager-delete-handler", data, true, function (response) {
                if (response.status == 0) {
                    filter();
                } else {
                    errorDialog(response.message);
                }
            });
        });
    }

    function initPassword(id) {
        $("#passwordModal").find("#id").val(id);
        $("#passwordModal").find("#password").val("");
        $("#passwordModal").modal('show');
    }

    function savePassword() {
        var id = $("#passwordModal").find("#id").val();
        var password = $("#passwordModal").find("#password").val().trim();

        if (validatePassword(password) == false) {
            return false;
        }


        var data = {};
        data.id = id;
        data.password = password;
        requestJson("manager-save-password-handler", data, true, function (response) {
            if (response.status == 0) {
                filter();
            } else {
                errorDialog(response.message);
            }
        });
    }

</script>

<div class="main-title">
    <div class="title-txt"><?= $this->title ?></div>
</div>

<!--search-->
<div class="panel panel-frame panel-operat">
    <div class="panel-body">
        <div class="panel-content">
            <input type="text" class="form-control search-box" placeholder="输入名称" id="name" name="name"
                   value="<?= $name ?>">
            <input type="text" class="form-control search-box" placeholder="输入账号" id="account" name="account"
                   value="<?= $account ?>">
            <input type="text" class="form-control search-box" placeholder="输入电话号码" id="phone" name="phone"
                   value="<?= $phone ?>">
            <a href="javascript:filter()" type="button" class="btn btn-warning search-icon">
                <div class="btn-icon"></div>
                <div class="btn-text">搜索</div>
            </a>
            <a href="javascript:onAddData()" type="button" class="btn btn-primary add-icon ">
                <div class="btn-icon"></div>
                <div class="btn-text">添加</div>
            </a>
        </div>
    </div>
</div>

<!--列表-->
<div class="panel panel-frame">
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>名称</th>
                <th>账号</th>
                <th>电话</th>
                <th>备注</th>
                <th>角色</th>
                <th>是否启用</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php
            for ($i = 0; $i < count($items); $i++) {
                $item = $items[$i];
                ?>
                <tr>
                    <td><?= $item['name'] ?></td>
                    <td><?= $item['account'] ?></td>
                    <td><?= $item['phone'] ?></td>
                    <td><?= $item['remark'] ?></td>
                    <td>
                    <?php foreach ($item['role'] as $role): ?>
                        <?php if ($role['role_name']): ?>
                        <span class="label label-<?= $role['role_is_enabled'] == 1 ? 'success' : 'danger' ?>">
                            <?= $role['role_name'] ?>
                        </span>
                        <?php endif; ?>
                    <?php endforeach; ?>
                    </td>
                    <td>
                        <span class="label label-<?= $item['is_enabled'] == 1 ? 'success' : 'danger' ?>">
                            <?= $item['is_enabled'] == 1 ? '已启用' : '已停用' ?>
                        </span>
                    </td>

                    <td class="table-text-align-left">
                        <?php if ($item['id'] != '0') { ?>
                            <a class="btn btn-link" href="javascript:deleteMiniPrograms('<?= $item['id'] ?>')">已有小程序</a>
                            <a class="btn btn-link" href="javascript:addMiniPrograms('<?= $item['id'] ?>')">添加小程序</a>
                            <a class="btn btn-link" href="javascript:onAddData('<?= $item['id'] ?>')">编辑</a>
                            <a class="btn btn-link" href="javascript:initPassword('<?= $item['id'] ?>')">初始化密码</a>
                            <a class="btn btn-link" href="javascript:deleteManager('<?= $item['id'] ?>')">删除</a>

                            <?php
                        } ?>
                    </td>
                </tr>
                <?php

            }
            ?>
            </tbody>
        </table>
    </div>
    <div class="table-pagination">
        <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
    </div>
</div>


<!--编辑-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">管理员管理</h4>
            </div>
            <div id="modaledit" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="managerMiniProgramModal" tabindex="-1" role="dialog" aria-labelledby="managerMiniProgramModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="managerMiniProgramModalLabel">添加小程序</h4>
            </div>
            <div id="managerMiniProgramModalEdit" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveMiniProgramData()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="managerDeleteMiniProgramModal" tabindex="-1" role="dialog" aria-labelledby="managerDeleteMiniProgramModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="managerDeleteMiniProgramModalLabel">删除小程序</h4>
            </div>
            <div id="managerDeleteMiniProgramModalEdit" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="deleteMiniProgramData()"><i class="fa fa-save"></i> 删除
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<!--角色-->
<div class="modal fade" id="roleModel" tabindex="-1" role="dialog" aria-labelledby="roleModelLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="roleModelLabel">管理员角色</h4>
            </div>
            <div id="roleModelEdit" >
            </div>
            <div class="modal-footer">
                <a type="button" class="btn btn-primary" onclick="saveManagerRole()"><i class="fa fa-save"></i> 保存
                </a>
                <a type="button" class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<!--初始化密码-->
<div class="modal fade" id="passwordModal" tabindex="-1" role="dialog" aria-labelledby="passwordModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="passwordModalLabel">初始化密码</h4>
            </div>
            <div id="passwordEdit" class="modal-body">
                <div class="form-horizontal" id="manager_edit_form">
                    <div class="form-group" style=" margin-top: 5px">
                        <label class="control-label col-lg-2" for="password">输入密码：</label>
                        <input type="hidden" id="id" name="id" class="form-control"
                               value="">
                        <div class="col-lg-10">
                            <input type="password" id="password" name="password" class="form-control"
                                   value="">
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="savePassword()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

</script>